package com.room.search.web.rest;

import com.room.search.domain.enumeration.file.ImageType;
import com.room.search.response.RoomResult;
import com.room.search.service.RoomFileService;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * REST controller for managing {@link com.room.search.domain.RoomFile}.
 */
@RestController
@RequestMapping("/api/file")
public class RoomFileResource {

    private final Logger log = LoggerFactory.getLogger(RoomFileResource.class);


    private final RoomFileService roomFileService;

    public RoomFileResource(RoomFileService roomFileService) {
        this.roomFileService = roomFileService;
    }

    @ApiModelProperty("小区或者房屋的图片上传")
    @PostMapping("/community/image_upload/{id}")
    public RoomResult communityImageUpload(@PathVariable Long id, MultipartFile file) {
        log.info("小区或者房屋的ID：{}，文件名：{}", id, file.getOriginalFilename());
        return roomFileService.imageUpload(id, file, ImageType.COMMUNITY);
    }

    @PostMapping("/goods/image_upload/{id}")
    public RoomResult goodsImageUpload(@PathVariable Long id, MultipartFile file) {
        log.info("小区或者房屋的ID：{}，文件名：{}", id, file.getOriginalFilename());
        return roomFileService.imageUpload(id, file, ImageType.GOODS);
    }


    @ApiModelProperty("文件下载")
    @GetMapping("/download/{id}")
    public ResponseEntity<Byte[]> downloadFile(@PathVariable Long id) {
        return roomFileService.downloadFile(id);
    }

    @ApiModelProperty("删除文件")
    @DeleteMapping("/delete/{id}")
    public RoomResult deleteFile(@PathVariable Long id) {
        return roomFileService.deleteFile(id);
    }

    @ApiModelProperty("根据communityId获取相应的文件")
    @GetMapping("/community/{id}")
    public RoomResult getFileByCommunity(@PathVariable Long id) {
        return roomFileService.getFileByCommunity(id);
    }

    @ApiModelProperty("根据communityId获取相应的文件")
    @GetMapping("/goods/{id}")
    public RoomResult getFileByGoods(@PathVariable Long id) {
        return roomFileService.getFileByGoods(id);
    }

    @ApiModelProperty("上次图片文件专属")
    @PostMapping("/upload")
    public RoomResult uploadFile(MultipartFile file) {
        log.info("上次文件：{}", file);
        return roomFileService.upload(file);
    }
}
